package com.bawei.animator

import android.animation.ObjectAnimator
import android.animation.TypeEvaluator
import android.animation.ValueAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.animation.BounceInterpolator
import com.bawei.animator.databinding.ActivityMain2Binding

/**
 *  差值器和估值器：
1。差值器：8大差值器，动画变化的规律：匀速 加速 减速 弹球效果
2。估值器：动画变化的值，比如说 0->100  fraction:0-1 贝塞尔曲线
 */
class MainActivity2 : AppCompatActivity() {
    private lateinit var activityMain2Binding: ActivityMain2Binding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMain2Binding = ActivityMain2Binding.inflate(layoutInflater)
        setContentView(activityMain2Binding.root)
        //差值器
        var translationX = ObjectAnimator.ofFloat(activityMain2Binding.iv,"translationX",0F,300F)
        translationX.duration = 3000
        translationX.interpolator = BounceInterpolator()
        translationX.start()
        //估值器
//        var valueAnimator = ValueAnimator.ofObject(MyEvaluator(),0,1000)
//        valueAnimator.duration = 2000
//        valueAnimator.addUpdateListener {
//            Log.d("ytx", "${it.animatedFraction}:${it.animatedValue}" )
//        }
//        valueAnimator.start()
        //贝塞尔曲线公式

        var valueAnimator2 = ValueAnimator.ofObject(BezierEvaluator(400,700),0,1000)
        valueAnimator2.duration = 2000
        valueAnimator2.addUpdateListener {
            Log.d("ytx", "${it.animatedFraction}:${it.animatedValue}" )
        }
        valueAnimator2.start()




    }
    inner class MyEvaluator:TypeEvaluator<Int>{
        /**
         * 0-1000
         * fraction:0-1  0.5 500 0.7
         * startValue:起始值
         * endValue：终点值
         */
        override fun evaluate(fraction: Float, startValue: Int?, endValue: Int?): Int {
            //匀速
            if(startValue != null && endValue != null){
                var  cha = endValue.minus(startValue)//差值 1000
                var fen = cha/100
                return startValue.plus((fraction*100*fen).toInt())
            }
            return 0

        }

    }


}